package com.example.rentingapp.activity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationHouseEntity;
import com.example.rentingapp.enetity.InformationHouseResponse;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.util.StringUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderPublishActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_back;
    private Button btn_publish;

    private TextView tvLandlordName, tvTenantName, tvHomeType, tvAddress, tvRent;
    private EditText etCheckIn, etCheckOut;
    private ImageView imgContract;
    private String orders_tenant_id, orders_landlord_id, orders_house_id, checkIn, checkOut;

    private List<InformationHouseEntity> datas = new ArrayList<>();
    private List<InformationEntity> datas2 = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    orders_landlord_id = String.valueOf(datas.get(0).getHouse_user_id());

                    tvLandlordName.setText(datas.get(0).getName());//房东名字
                    tvAddress.setText(datas.get(0).getAddress());
                    tvHomeType.setText(String.valueOf(datas.get(0).getHome_type()));
                    tvRent.setText(String.valueOf(datas.get(0).getHouse_rent()));
                    break;
                case 1:
                    tvTenantName.setText(datas2.get(0).getName());//房客名字
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.publish_myorder;
    }

    @Override
    protected void initView() {
        iv_back = findViewById(R.id.iv_back_to_housingdtails);
        btn_publish = findViewById(R.id.btn_house_update);

        tvLandlordName = findViewById(R.id.tv_landlord_name);
        tvTenantName = findViewById(R.id.tv_tenant_name);
        tvHomeType = findViewById(R.id.tv_home_type);
        tvAddress = findViewById(R.id.tv_address);
        tvRent = findViewById(R.id.tv_rent);
        etCheckIn = findViewById(R.id.et_check_in);
        etCheckOut = findViewById(R.id.et_check_out);
        imgContract = findViewById(R.id.img_contract);
    }

    @Override
    protected void initData() {
        iv_back.setOnClickListener(this);
        btn_publish.setOnClickListener(this);

        orders_house_id = findByKey("house_id");
        orders_tenant_id = findByKey("user_id");

        getOrderDetails();//查看自动生成的订单
        getTenantName();//查看房客名字
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_to_housingdtails://返回按钮
                finish();
                break;
            case R.id.btn_house_update://发布订单接口实现
                if (orders_landlord_id.equals(orders_tenant_id)) {
                    showToast("用户无法租自己的房子");
                } else {
                    publishOrders();//下订单
                }
                finish();
                break;
        }
    }

    //查看自动生成的订单
    private void getOrderDetails() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_id", orders_house_id);
        Api.config(ApiConfig.FIND_INFORMATION_HOUSE, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationHouseResponse response = gson.fromJson(res, InformationHouseResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationHouseEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("生成失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    //查看房客姓名
    private void getTenantName() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("information_user_id", orders_tenant_id);
        Api.config(ApiConfig.FIND_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas2 = list;
                        mHandler.sendEmptyMessage(1);
                    }
                } else {
                    showToastSync("未找到房客姓名");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    //下订单
    private void publishOrders() {
        checkIn = etCheckIn.getText().toString().trim();
        checkOut = etCheckOut.getText().toString().trim();

        if (StringUtils.isEmpty(checkIn)) {
            showToast("请输入入住时间");
            return;
        }
        if (StringUtils.isEmpty(checkOut)) {
            showToast("请输入退房时间");
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orders_tenant_id", orders_tenant_id);
        params.put("orders_landlord_id", orders_landlord_id);
        params.put("orders_house_id", orders_house_id);
        params.put("check_orders", 'N');
        params.put("check_in", checkIn);
        params.put("check_out", checkOut);
        Api.config(ApiConfig.PUBLISH_ORDERS, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                showToastSync(res);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}